#include "mainWindow.h"

#include <QtGui/QAction>
#include <QtGui/QMenuBar>
#include <QtGui/QMenu>
#include <QtGui/QToolBar>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QLabel>
#include <QtGui/QtGui>
#include <QtGui/QMouseEvent>
#include <QtGui/QTableWidget>

const QEvent::Type MYCOSTOMEVEVT= (QEvent::Type)5001;

SpreadSheet::SpreadSheet(QWidget* parent /* = 0 */)
	:QMainWindow(parent),
	printAction(NULL),
	m_msgLabel(NULL)
{
	resize(800, 600);

	int rows = 26;
	int cols = 26;
	table = new QTableWidget(26, 26, this);
	for (int c = 0; c < cols; ++c) {
		QString character(QChar('A' + c));
		table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));
	}

	createActions();
	createMenuBar();
	createToolBars();
	createStatusBar();

	//connect(printAction, SIGNAL(triggered()), this, SLOT(openFile()));

	setCentralWidget(table);

}

SpreadSheet::~SpreadSheet()
{

}

void SpreadSheet::createActions()
{
	printAction = new QAction(tr("&Print"), this);
	printAction->setShortcut(QKeySequence::Print);
	printAction->setStatusTip(tr("Print a file"));

	exitAction = new QAction(tr("&Exit"), this);
	exitAction->setShortcut(QKeySequence::Quit);
	connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

	cell_addAction = new QAction(tr("&Add"), this);
	cell_addAction->setShortcut(Qt::CTRL | Qt::Key_Plus);

	cell_subtractAction = new QAction(tr("&Subtract"), this);
	cell_subtractAction->setShortcut(Qt::CTRL | Qt::Key_Minus);

	cell_multiplyAction = new QAction(tr("&Multiply"), this);
	cell_multiplyAction->setShortcut(Qt::CTRL | Qt::Key_multiply);

	cell_divideAction = new QAction(tr("&Divide"), this);
	cell_divideAction->setShortcut(Qt::CTRL | Qt::Key_division);

	cell_sumAction = new QAction(tr("Sum"), this);

	fontAction = new QAction(tr("Font"), this);
	connect(fontAction, SIGNAL(triggered()), this, SLOT(selectFont()));

	colorAction = new QAction(tr("Background"), this);
	connect(colorAction, SIGNAL(triggered()), this, SLOT(selectColor()));
}

void SpreadSheet::createMenuBar()
{
	QMenu* file = menuBar()->addMenu(tr("&File"));
	file->addAction(printAction);
	file->addAction(exitAction);

	QMenu* cell = menuBar()->addMenu(tr("&Cell"));
	cell->addAction(cell_addAction);
	cell->addAction(cell_subtractAction);
	cell->addAction(cell_multiplyAction);
	cell->addAction(cell_divideAction);
	cell->addAction(cell_sumAction);
	cell->addSeparator();
	cell->addAction(colorAction);
	cell->addAction(fontAction);
}

void SpreadSheet::createToolBars()
{
	QToolBar* toolbar = addToolBar(tr("Cell"));

	cellLabel = new QLabel(toolbar);
	cellLabel->setMinimumSize(80, 0);

	formulaEdit = new QLineEdit(toolbar);

	toolbar->addWidget(cellLabel);
	toolbar->addWidget(formulaEdit);
}

void SpreadSheet::createStatusBar()
{
	m_msgLabel = new QLabel(this);
	m_msgLabel->setMinimumSize(m_msgLabel->sizeHint());
	m_msgLabel->setAlignment(Qt::AlignCenter);

	statusBar()->addWidget(m_msgLabel);
}

void SpreadSheet::selectColor()
{
	QTableWidgetItem *item = table->currentItem();
	QColor col = item ? item->backgroundColor() : table->palette().base().color();
	col = QColorDialog::getColor(col, this);
	if (!col.isValid())
		return;
	
	QList<QTableWidgetItem*> selectitems = table->selectedItems();
	if (selectitems.empty())
		return;

	foreach(QTableWidgetItem* pItem, selectitems)
	{
		if (pItem)
			pItem->setBackgroundColor(col);
	}

	updateColor(item);
}

void SpreadSheet::updateColor(QTableWidgetItem* pItem)
{
	QColor color;
	if (pItem)
		color = pItem->backgroundColor();

	if (!color.isValid())
		color = palette().base().color();

	QPixmap pix(16, 16);
	QPainter painter(&pix);
	painter.fillRect(0, 0, 16, 16, color);

	QColor lighter = color.light();
	painter.setPen(lighter);
	QPoint lightFrame[] = { QPoint(0, 15), QPoint(0, 0), QPoint(15, 0) };
	painter.drawPolyline(lightFrame, 3);

	painter.end();
}

void SpreadSheet::selectFont()
{
	QList<QTableWidgetItem*> selectitems = table->selectedItems();
	if (selectitems.empty())
		return;

	bool bOk;
	QFont ft = QFontDialog::getFont(&bOk, this->font(), this);
	if (!bOk)
		return;

	foreach(QTableWidgetItem* pItem, selectitems)
	{
		if (pItem)
			pItem->setFont(ft);
	}
}